import VueRouter from 'vue-router'
import Vue from 'vue'
Vue.use(VueRouter)

// 导入页面组件
// import Home from '@/views/Home'
// import Cart from '@/views/Cart'
// import User from '@/views/User'
// import Topic from '@/views/Topic'
// import Category from '@/views/Category'
// import searchPop from '@/views/searchPop'
// import Detail from '@/views/Detail'
// 创建路由配置
const routes = [
    {path:'/' , redirect:'/home' },
    {path:'/home' , component:() => import('@/views/Home'), name:'home'  , 
     children:[
         {path:'searchpop', component:() => import('@/views/searchPop'), name:'searchpop' ,meta:{APPTarBar:false}}
     ],
     meta:{APPTarBar:true}    
    },
    {path:'/cart' , component:() => import('@/views/Cart'), name:'cart' ,meta:{APPTarBar:true}   },
    {path:'/category' , component:() => import('@/views/Category'), name:'category' ,meta:{APPTarBar:true}},
    {path:'/topic' , component:() => import('@/views/Topic'), name:'topic' ,meta:{APPTarBar:true}},
    {path:'/user' , component:() => import('@/views/User'), name:'user' ,meta:{APPTarBar:true}},
    {path:'/detail' , component:() => import('@/views/Detail'), name:'detail' ,meta:{APPTarBar:false}},
]



// 创建路由对象实例对象

const router = new VueRouter({
    mode:'history',
    routes
})


// 添加路由守卫
router.beforeEach((to,from,next)=>{
    // 判断是跳转到购物车页面
   
    
    // if(to.path == '/cart'){
    //     // 创建变量获取localstorage存储的user
    //     let user = localStorage.getItem('user')
    //     // 判断user里面有东西还是 null
    //     if(user){
    //         // 有信息正常放行
    //         next()
    //     }else if(user == null){
    //         // 没信息跳转登录页面
    //         // this.$toast('请先登录')
    //         next()
    //     }else{
    //         next('/login')
    //     }
    // }
    if(to.path == '/cart'){
        let user  = localStorage.getItem('user')
        if(user){
            next()
        }else{
            Vue.prototype.$toast.fail('请先登录..')

            setTimeout(() =>{
                next('/user')
            },1000)
           
        }
    }else{
        next()
    }
})









export default router